package com.hhs.storage.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hhs.storage.bean.*;
import com.hhs.storage.dao.UserDao;
import com.hhs.storage.service.*;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hhs.storage.util.StringUtil;
import com.hhs.storage.util.TreeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author shadow
 * @since 2018-09-29
 */
@Service
@Slf4j
public  class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Resource
    JxcRoleUserService jxcRoleUserService;
    @Resource
    YymUserCjLinkService yymUserCjLinkService;
    @Resource
    YymUserExtService yymUserExtService;
    @Resource
    YymUserTcLinkService yymUserTcLinkService;
    @Resource
    YymUserYijianService yymUserYijianService;
    @Resource
    YymSmsTemplateService yymSmsTemplateService;
    @Override
    public Map<String, Object> saveOrUpdate(User user, String roleStr) {
        List<User> tempList = this.baseMapper.selectList(new EntityWrapper<User>().eq("account_", user.getAccount()));
        //新增
        if (StringUtils.isEmpty(user.getId())) {
            if(StringUtils.isEmpty(user.getPassword()))
            user.setPassword("123456");//初始化密码
            user.setCreateTime(new Date());
            //要确保账号的唯一性，判断账号是否已经存在
            if (tempList.size() > 0) {
                return Result.failJsonResp("账号已存在，请重新输入");
            }
        } else {//修改
            if (tempList.size() > 0 && !tempList.get(0).getId().equals(user.getId())) {
                return Result.failJsonResp("账号已存在，请重新输入");
            }

        }
        this.insertOrUpdate(user);

        return Result.sucJsonResp(null);
    }

    @Override
    public Map<String, Object> dele(String id) {
        //先删除关联角色数据
        jxcRoleUserService.delete(new EntityWrapper<JxcRoleUser>().eq("user_id_", id));
        this.delete(new EntityWrapper<User>().eq("id_", id));
        //用户采集link
        yymUserCjLinkService.delete(new EntityWrapper<YymUserCjLink>().eq("user_id_",id));
        //用户扩展
        yymUserExtService.delete(new EntityWrapper<YymUserExt>().eq("user_id_",id));
        //用户套餐
        yymUserTcLinkService.delete(new EntityWrapper<YymUserTcLink>().eq("user_id_",id));
        //用户意见
        yymUserYijianService.delete(new EntityWrapper<YymUserYijian>().eq("user_id_",id));
        //用户模板
        yymSmsTemplateService.delete(new EntityWrapper<YymSmsTemplate>().eq("user_id_",id));
        return null;
    }

    @Override
    public User aa(String id) {
        return this.baseMapper.aa(id);
    }

    @Override
    public Map<String, Object> editRole(String userId, String[] roleArr) {
        //删除旧的关联数据
       jxcRoleUserService.delete(new EntityWrapper<JxcRoleUser>().eq("user_id_", userId));
        //新增用户与角色关联
            for (String s : roleArr) {
                JxcRoleUser jru = new JxcRoleUser();
                jru.setUserId(userId);
                jru.setRoleId(s);
                jru.setCreateTime(new Date());
                jxcRoleUserService.insert(jru);
            }
        return Result.sucJsonResp(null);
    }

    @Override
    public Map<String, Object> modifypass(String oldpass, String newpass) {
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        EntityWrapper<User> fu=new EntityWrapper<>(new User());
        fu.eq("account_",user.getAccount());
        List<User> tempList = this.baseMapper.selectList(fu);
        if(tempList.size()==0){
           return Result.failJsonResp("没有找到账号！");
        }else{
            User u=tempList.get(0);
            if(!StringUtil.zNull(u.getPassword()).equals(oldpass)){
                return Result.failJsonResp("原密码不正确！");
            }
            u.setPassword(newpass);
            this.updateById(u);
            return Result.sucJsonResp(null);
        }
    }
    @Override
    public AppUserAllInf selectAllInf(String userid){
        return this.baseMapper.selectAllInf(userid);
    }

    @Override
    public Page<User> findPageChild(InputParam inputParam) {
        Page<User> page = new Page(inputParam.getPage(), inputParam.getSize());

        EntityWrapper<User> ew=new EntityWrapper<>();
        ew.in("id_",inputParam.getSubids());
        return page.setRecords(this.baseMapper.selectList(ew));
    }

    @Override
    public String findIds(InputParam inputParam) {
        String param=StringUtil.zNull(inputParam.getSubids());
        User userModel = (User)SecurityUtils.getSubject().getSession().getAttribute("user");
        if(StringUtils.isEmpty(param)||"''".equals(param)) {
            List<User> alluser = this.baseMapper.selectList(new EntityWrapper<User>());
            List<TreeUtil.Node> allnode = new ArrayList<>();
            //找到所有子结点
            for (User user : alluser) {
                TreeUtil.Node node = new TreeUtil.Node();
                node.setId(user.getId());
                node.setPid(user.getPid());
                node.setName(user.getName());
                allnode.add(node);
            }
            TreeUtil.NodeRecursion treeutil = TreeUtil.getInstance().new NodeRecursion();
            List<TreeUtil.Node> subnodelist = treeutil.treeNodeList(allnode, userModel.getId());
            List<String> temp = new ArrayList<>();
            for (TreeUtil.Node node :
                    subnodelist) {
                temp.add(node.getId());
            }
            param = StringUtil.toCommaString(temp);
            if(StringUtil.iszNull(param)){
                param="''";
            }
        }
        return param;
    }

    @Override
    public int findonelever(InputParam inputParam) {
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        inputParam.setUserid(user.getId());
        int result=this.baseMapper.findonlever(inputParam);
        return result;
    }

    @Override
    public int findmaxcount(String id) {
        return this.baseMapper.findmaxcount(id);
    }
}
